##
# Replication for The Element of Surprise: Election Timing and Opposition Preparedness
##

# Charles T. McClean
# Comparative Political Studies Dataverse

##
# Packages
##

library(tidyverse)
library(lfe)
library(stargazer)

##
# Files
##

hor <- readRDS("hor_elections.RDS")
by <- readRDS("by_elections.RDS")
timing <- readRDS("timing.RDS")
factions <- readRDS("faction_candidates.RDS")

##
# Table 1: The LDP Receives More Votes in Surprise Elections
##

m1 <- felm(ldp_vs ~ surprise | 0 | 0 | dcode + year, data=hor) 
m2 <- felm(ldp_vs ~ surprise | dcode + quarter | 0 | dcode + year, data=hor) 
m3 <- felm(ldp_vs ~ surprise + quarter_no + since_uh + since_local | dcode + quarter | 0 | dcode + year, data=hor) 
m4 <- felm(ldp_vs ~ surprise + quarter_no + since_uh + since_local +
             pre_vs + pre_seat + ldp_support_avg_lag1 | dcode + quarter | 0 | dcode + year, data=hor)
m5 <- felm(ldp_vs ~ surprise + quarter_no + since_uh + since_local +
             pre_vs + pre_seat + ldp_support_avg_lag1 + gdp_growth_lag1 + inflation_lag1 | dcode + quarter | 0 | dcode + year, data=hor)

stargazer(m1,m2,m3,m4,m5, type="text",
          dep.var.labels = c("LDP Vote Share"),
          omit.stat = c("f", "ser", "adj.rsq"),
          digits=2,
          covariate.labels = c("Surprise Election", 
                               "Linear Time Trend", "Quarters Since Upper House Election", "Quarters Since Unified Local Election",
                               "Previous LDP Vote Share", "Previous LDP Seat Share", "LDP Support Rating (lagged 1 quarter)",
                               "GDP Growth (lagged 1 quarter)", "Inflation (lagged 1 quarter)"),
          title="The LDP Receives More Votes in Surprise Elections")

##
# Table 2: Surprise Elections Undermine Opposition Preparedness
##

m1 <- felm(opp_recruit ~ surprise + pre_opp_recruit | dcode + quarter | 0 | dcode+year, data=hor)
m2 <- felm(opp_recruit ~ surprise + quarter_no + since_uh + since_local +
             pre_opp_recruit + ldp_support_avg_lag1 + pre_seat + gdp_growth_lag1 + inflation_lag1 | dcode + quarter | 0 | dcode + year, data=hor)
m3 <- felm(opp_hq ~ surprise + pre_opp_hq | dcode + quarter | 0 | dcode + year, data=hor, subset=year!=2017) 
m4 <- felm(opp_hq ~ surprise + quarter_no + since_uh + since_local +
             pre_opp_hq + ldp_support_avg_lag1 + pre_seat + gdp_growth_lag1 + inflation_lag1 | dcode + quarter | 0 | dcode + year, data=hor, subset=year!=2017) 
m5 <- felm(opp_over ~ surprise + pre_opp_over | dcode + quarter | 0 | dcode + year, data=hor)
m6 <- felm(opp_over ~ surprise + quarter_no + since_uh + since_local +
             pre_opp_over + ldp_support_avg_lag1 + pre_seat + gdp_growth_lag1 + inflation_lag1 | dcode + quarter | 0 | dcode + year, data=hor)
m7 <- felm(opp_exp_pc ~ surprise + pre_opp_exp_pc | dcode + quarter | 0 | dcode + year, data=hor)
m8 <- felm(opp_exp_pc ~ surprise + quarter_no + since_uh + since_local +
             pre_opp_exp_pc + ldp_support_avg_lag1 + pre_seat + gdp_growth_lag1 + inflation_lag1 | dcode + quarter | 0 | dcode + year, data=hor)

stargazer(m1,m2,m3,m4,m5,m6,m7,m8, type="text",
          dep.var.labels = c("Recruited Candidates", "High Quality Recruits", "Coordination Error", "Campaign Spending"),
          omit.stat = c("f", "ser", "adj.rsq"),
          digits=2,
          covariate.labels = c("Surprise Election", 
                               "Linear Time Trend", "Quarters Since Upper House Election", "Quarters Since Unified Local Election",
                               "Previous Number of Candidates", "LDP Support Rating (lagged 1 quarter)", "Previous LDP Seat Seat Share",
                               "GDP Growth (lagged 1 quarter)", "Inflation (lagged 1 quarter)",
                               "Previous High Quality Candidates", "Previous Coordination Error", "Previous Campaign Spending"),
          title="Surprise Elections Undermine Oppositon Preparedness")

##
# Table 3: Surprise Matters More in Competitive Districts
##

m1 <- felm(ldp_vs ~ surprise * pre_gap + pre_vs | dcode | 0 | dcode + year, data=hor)
m2 <- felm(ldp_vs ~ surprise * pre_gap + pre_vs + pre_seat | dcode + year | 0 | dcode + year, data=hor)
m3 <- felm(ldp_vs ~ surprise * pre_opp_over + pre_vs | dcode | 0 | dcode + year, data=hor)
m4 <- felm(ldp_vs ~ surprise * pre_opp_over + pre_vs + pre_seat | dcode + year | 0 | dcode + year, data=hor)
m5 <- felm(ldp_vs ~ surprise * same_faction_pct + pre_vs | dcode | 0 | dcode + year, data=hor, subset=hor$year != 2017)
m6 <- felm(ldp_vs ~ surprise * same_faction_pct + pre_vs + pre_seat | dcode + year | 0 | dcode + year, data=hor, subset=hor$year != 2017)

stargazer(m1,m2,m3,m4,m5,m6, type="text",
          dep.var.labels = c("LDP Vote Share"),
          omit.stat = c("f", "ser", "adj.rsq"),
          digits=2,
          covariate.labels = c("Surprise Election", 
                               "Previous Margin of Victory", "Previous Coordination Error", "Cabinet Faction Ties (%)",
                               "Previous LDP Vote Share", "Previous LDP Seat Share",
                               "Surprise x Prev. Margin of Victory", "Surprise x Prev Coordination Error", "Surprise x Faction Ties (%)"),
          title="Surprise Matters More in Competitive Districts")

##
# Table 4: Surprise Matters More in Shorter Election Campaigns
##

m1 <- felm(ldp_vs ~ surprise * koji + pre_vs | dcode + quarter | 0 | dcode + year, data=hor)
m2 <- felm(ldp_vs ~ surprise * koji + quarter_no + since_uh + since_local +
             pre_vs + ldp_support_avg_lag1 + pre_seat + gdp_growth_lag1 + inflation_lag1 | dcode + quarter | 0 | dcode + year, data=hor)
m3 <- felm(ldp_vs ~ surprise * reform + pre_vs | quarter | 0 | dcode + year, data=hor)
m4 <- felm(ldp_vs ~ surprise * reform + quarter_no + since_uh + since_local +
             pre_vs + ldp_support_avg_lag1 + pre_seat + gdp_growth_lag1 + inflation_lag1 | dcode + quarter | 0 | dcode + year, data=hor)
m5 <- felm(ldp_vs ~ surprise * years_early + pre_vs | dcode + quarter | 0 | dcode + year, data=hor)
m6 <- felm(ldp_vs ~ surprise * years_early + quarter_no + since_uh + since_local +
             pre_vs + ldp_support_avg_lag1 + pre_seat + gdp_growth_lag1 + inflation_lag1 | dcode + quarter | 0 | dcode + year, data=hor)

stargazer(m1,m2,m3,m4,m5,m6, type="text",
          dep.var.labels = c("LDP Vote Share"),
          omit.stat = c("f", "ser", "adj.rsq"),
          digits=2,
          covariate.labels = c("Surprise Election", 
                               "Campaign Length", "Linear Time Trend", "Since Upper House Election", "Since Unified Local Election",
                               "Electoral Reform", "Years Early",
                               "Previous LDP Vote Share", "LDP Support Rating (lagged 1 quarter)", "Previous LDP Seat Share",
                               "GDP Growth (lagged 1 quarter)", "Inflation (lagged 1 quarter)",
                               "Surprise x Campaign Length", "Surprise x Electoral Reform", "Surprise x Years Early"),
          title="Surprise Matters More in Shorter Election Campaigns")

##
# Table 5: Surprise By-Elections Benefit the LDP and Inhibit Opposition Preparedness
##

m1 <- felm(ldp_vs ~ surprise + pre_vs | dcode + quarter | 0 | dcode + year, data=by)
m2 <- felm(ldp_vs ~ surprise + since_uh + since_local +
             pre_vs + ldp_support_avg_lag1 + pre_seat + gdp_growth_lag1 + inflation_lag1 | dcode + quarter | 0 | dcode + year, data=by)
m3 <- felm(opp_recruit ~ surprise + pre_opp_recruit | dcode + quarter | 0 | dcode + year, data=by)
m4 <- felm(opp_recruit ~ surprise + since_uh + since_local +
             pre_opp_recruit +  ldp_support_avg_lag1 + pre_seat + gdp_growth_lag1 + inflation_lag1 | dcode + quarter | 0 | dcode + year, data=by)
m5 <- felm(opp_hq ~ surprise + pre_opp_hq | dcode + quarter | 0 | dcode + year, data=by)
m6 <- felm(opp_hq ~ surprise + since_uh + since_local +
             pre_opp_hq + ldp_support_avg_lag1 + pre_seat + gdp_growth_lag1 + inflation_lag1 | dcode + quarter | 0 | dcode + year, data=by)
m7 <- felm(opp_over ~ surprise + pre_opp_over | dcode + quarter | 0 | dcode + year, data=by)
m8 <- felm(opp_over ~ surprise + since_uh + since_local +
             pre_opp_over + ldp_support_avg_lag1 + pre_seat + gdp_growth_lag1 + inflation_lag1 | dcode + quarter | 0 | dcode + year, data=by)

stargazer(m1,m2,m3,m4,m5,m6,m7,m8, type="text",
          dep.var.labels = c("LDP Vote Share", "Recruited Candidates", "High Quality Recruits", "Coordination Error"),
          omit.stat = c("f", "ser", "adj.rsq"),
          digits=2,
          covariate.labels = c("Surprise By-Election", 
                               "Since Upper House Election", "Since Unified Local Election",
                               "Previous LDP Vote Share", "LDP Support Rating (lagged 1 quarter)", "Previous LDP Seat Share", 
                               "GDP Growth (lagged 1 quarter)", "Inflation (lagged 1 quarter)",
                               "Previous Number of Candidates", "Previous High Quality Candidates", "Previous Coordination Error"),
          title="Surprise By-Elections Benefit the LDP and Inhibit Opposition Preparedness")

##
# Table A2: Balance Table for Surprise and Expected Elections
##

years <- select(hor, year, surprise, gdp_growth_lag1, gdp_growth_lag2, gdp_growth_lag3, gdp_growth_lag4, inflation_lag1, inflation_lag2, inflation_lag3, inflation_lag4,
                ldpseatshare, reform)
years <- unique(years)
head(years)

t.test(years$ldpseatshare[years$surprise == 1], years$ldpseatshare[years$surprise == 0])
t.test(years$year[years$surprise == 1], years$year[years$surprise == 0])
t.test(years$reform[years$surprise == 1], years$reform[years$surprise == 0])
t.test(years$gdp_growth_lag1[years$surprise == 1], years$gdp_growth_lag1[years$surprise == 0])
t.test(years$gdp_growth_lag2[years$surprise == 1], years$gdp_growth_lag2[years$surprise == 0])
t.test(years$gdp_growth_lag3[years$surprise == 1], years$gdp_growth_lag3[years$surprise == 0])
t.test(years$gdp_growth_lag4[years$surprise == 1], years$gdp_growth_lag4[years$surprise == 0])
t.test(years$inflation_lag1[years$surprise == 1], years$inflation_lag1[years$surprise == 0])
t.test(years$inflation_lag2[years$surprise == 1], years$inflation_lag2[years$surprise == 0])
t.test(years$inflation_lag3[years$surprise == 1], years$inflation_lag3[years$surprise == 0])
t.test(years$inflation_lag4[years$surprise == 1], years$inflation_lag4[years$surprise == 0])

##
# Table A3: Alternative Modeling Strategy
##

lm1 <- lm(lh_election ~ since_lh + since_uh + since_local + ldp_support_avg_lag1 + no_confidence + no_confidence_lag1 + no_confidence_passed + inflation_lag1 + gdp_growth_lag1 +
            ldp_seat_share + factor(quarter), data=timing)
glm1 <- glm(lh_election ~ since_lh + since_uh + since_local + ldp_support_avg_lag1 + no_confidence + no_confidence_lag1 + no_confidence_passed + inflation_lag1 + gdp_growth_lag1 +
              ldp_seat_share + factor(quarter), data=timing, family=binomial(link="logit"))
lm2 <- lm(surprise ~ residuals(lm1), data=timing)
glm2 <- glm(surprise ~ residuals(glm1), data=timing, family=binomial(link="logit"))
lm3 <- felm(ldp_vs ~ residuals_lm + quarter_no + since_uh + since_local +
              pre_vs + ldp_support_avg_lag1 + pre_seat + gdp_growth_lag1 + inflation_lag1 | dcode + quarter | 0 | dcode + year, data=hor)
glm3 <- felm(ldp_vs ~ residuals_glm + quarter_no + since_uh + since_local +
               pre_vs + ldp_support_avg_lag1 + pre_seat + gdp_growth_lag1 + inflation_lag1 | dcode + quarter | 0 | dcode + year, data=hor)

stargazer(lm1, glm1, lm2, glm2, lm3, glm3, type="text",
          dep.var.labels = c("Lower House Election", "Surprise Election", "LDP Vote Share"),
          omit.stat = c("f", "ser", "adj.rsq"),
          digits=2,
          covariate.labels = c("Quarters Since Lower House Election", "Residuals (M1)", "Residuals (M2)",
                               "Linear Time Trend", "Quarters Since Upper House Election", "Quarters Since Unified Local Election",
                               "Previous LDP Vote Share", "LDP Support Rating (lagged 1 quarter)",
                               "No Confidence Motion", "No Confidence Motion (lagged 1 quarter)", "Passed No Confidence Motion",
                               "Previous LDP Seat Share",
                               "Inflation (lagged 1 quarter)",
                               "GDP Growth (lagged 1 quarter)",
                               "LDP Seat Share",
                               "Quarter 2", "Quarter 3", "Quarter 4",
                               "Residuals (M1)", "Residuals (M2)"),
          title="Alternative Modeling Strategy With Residuals")

##
# Table A4: Alternative Time Controls
##

knots <- quantile(unique(hor$quarter_no), p=c(0.25, 0.5, 0.75))
library(splines)

m1 <- felm(ldp_vs ~ surprise + quarter_no + since_uh + since_local +
             pre_vs + ldp_support_avg_lag1 + pre_seat + gdp_growth_lag1 + inflation_lag1 | dcode + quarter | 0 | dcode + year, data=hor)
m2 <- felm(ldp_vs ~ surprise + poly(quarter_no, 2) + poly(since_uh, 2) + poly(since_local, 2) +
             pre_vs + ldp_support_avg_lag1 + pre_seat + gdp_growth_lag1 + inflation_lag1 | dcode + quarter | 0 | dcode + year, data=hor)
m3 <- felm(ldp_vs ~ surprise + poly(quarter_no, 3) + poly(since_uh, 3) + poly(since_local, 3) +
             pre_vs + ldp_support_avg_lag1 + pre_seat + gdp_growth_lag1 + inflation_lag1 | dcode + quarter | 0 | dcode + year, data=hor)
m4 <- felm(ldp_vs ~ surprise + bs(quarter_no, knots = knots)  + since_uh + since_local +
             pre_vs + ldp_support_avg_lag1 + pre_seat + gdp_growth_lag1 + inflation_lag1 | dcode + quarter | 0 | dcode + year, data=hor)

stargazer(m1,m2,m3,m4, type="text",
          dep.var.labels = c("LDP Vote Share"),
          omit.stat = c("f", "ser", "adj.rsq"),
          digits=2,
          covariate.labels = c("Surprise Election",
                               "Linear Time Trend",
                               "Time Trend Knots 1", "Time Trend Knots 2", "Time Trend Knots 3", "Time Trend Knots 4", "Time Trend Knots 5", "Time Knots 6",
                               "Quarters Since Upper House Election", "Quarters Since Unified Local Election",
                               "Time Trend", "Time Trend2", "Quarters Since Upper House Election", "Quarters Since Upper House Election2", "Quarters Since Unified Local Election", "Quarters Since Unified Local Election2",
                               "Time Trend", "Time Trend2", "Time Trend3", "Quarters Since Upper House Election", "Quarters Since Upper House Election2", "Quarters Since Upper House Election3", "Quarters Since Unified Local Election", "Quarters Since Unified Local Election2", "Quarters Since Unified Local Election3",
                               "Previous LDP Vote Share", "LDP Support Rating (lagged 1 quarter)", "Previous LDP Seat Share", "GDP Growth (lagged 1 quarter)", "Inflation (lagged 1 quarter)"),
          title="Alternative Time Controls")

##
# Table A5: Alternative Time Lags
##

m1 <- felm(ldp_vs ~ surprise + quarter_no + since_uh + since_local +
             pre_vs + ldp_support_avg_lag1 + pre_seat + gdp_growth_lag1 + inflation_lag1 | dcode + quarter | 0 | dcode + year, data=hor)
m2 <- felm(ldp_vs ~ surprise + quarter_no + since_uh + since_local +
             pre_vs + ldp_support_avg_lag2 + pre_seat + gdp_growth_lag2 + inflation_lag2 | dcode + quarter | 0 | dcode + year, data=hor)
m3 <- felm(ldp_vs ~ surprise + quarter_no + since_uh + since_local +
             pre_vs + ldp_support_avg_lag3 + pre_seat + gdp_growth_lag3 + inflation_lag3 | dcode + quarter | 0 | dcode + year, data=hor)
m4 <- felm(ldp_vs ~ surprise + quarter_no + since_uh + since_local +
             pre_vs + ldp_support_avg_lag4 + pre_seat + gdp_growth_lag4 + inflation_lag4 | dcode + quarter | 0 | dcode + year, data=hor)

stargazer(m1,m2,m3,m4, type="text",
          dep.var.labels = c("LDP Vote Share"),
          omit.stat = c("f", "ser", "adj.rsq"),
          digits=2,
          covariate.labels = c("Surprise Election", 
                               "Linear Time Trend", "Quarters Since Upper House Election", "Quarters Since Unified Local Election",
                               "Previous LDP Vote Share", 
                               "LDP Support Rating (lagged 1 quarter)", "LDP Support Rating (lagged 2 quarters)", "LDP Support Rating (lagged 3 quarters)", "LDP Support Rating (lagged 4 quarters)",
                               "Previous LDP Seat Share",
                               "GDP Growth (lagged 1 quarter)", "Inflation (lagged 1 quarter)",
                               "GDP Growth (lagged 2 quarters)", "Inflation (lagged 2 quarters)",
                               "GDP Growth (lagged 3 quarters)", "Inflation (lagged 3 quarters)",
                               "GDP Growth (lagged 4 quarters)", "Inflation (lagged 4 quarters)"),
          title="Alternative Time Lags")

##
# Table A6: Surprise Elections and Intraparty Competition (1958-2017)
##

m1 <- felm(ldp_vs ~ surprise * same_faction_pct + pre_vs + pre_seat | dcode + year | 0 | dcode + year, data=hor, subset=hor$year != 2017)
m2 <- felm(ldp_vs ~ surprise * pm_faction_pct + pre_vs + pre_seat | dcode + year | 0 | dcode + year, data=hor, subset=hor$year != 2017)
m3 <- felm(ldp_vs ~ surprise * same_faction_as_cabinet + pre_vs + pre_seat | dcode + year | 0 | dcode + year, data=factions, subset=factions$year != 2017)
m4 <- felm(ldp_vs ~ surprise * same_faction_as_PM + pre_vs + pre_seat | dcode + year | 0 | dcode + year, data=factions, subset=factions$year != 2017)

stargazer(m1,m2,m3,m4, type="text",
          dep.var.labels = c("LDP Vote Share"),
          omit.stat = c("f", "ser", "adj.rsq"),
          digits=2,
          covariate.labels = c("Surprise Election", 
                               "Cabinet Faction Ties (%)", "Prime Minister Faction Ties (%)", "Cabinet Faction Ties (%)", "Prime Minister Faction Ties (%)",
                               "Previous LDP Vote Share",  "Previous LDP Seat Share",
                               "Surprise x Cabinet Faction Ties (%)", "Surprise x Prime Minister Faction Ties (%)", "Surprise x Cabinet Faction Ties (%)", "Surprise x Prime Minister Faction Ties (%)"),
          title="Surprise Elections and Intraparty Competition (1958-2017)")

##
# Table A7: Surprise Elections and Intraparty Competition Prior to Reform (1958-1993)
##

m1 <- felm(ldp_vs ~ surprise * same_faction_pct + pre_vs + pre_seat | dcode + year | 0 | dcode + year, data=hor, subset=hor$year < 1994)
m2 <- felm(ldp_vs ~ surprise * pm_faction_pct + pre_vs + pre_seat | dcode + year | 0 | dcode + year, data=hor, subset=hor$year < 1994)
m3 <- felm(ldp_vs ~ surprise * same_faction_as_cabinet + pre_vs + pre_seat | dcode + year | 0 | dcode + year, data=factions, subset=factions$year < 1994)
m4 <- felm(ldp_vs ~ surprise * same_faction_as_PM + pre_vs + pre_seat | dcode + year | 0 | dcode + year, data=factions, subset=factions$year < 1994)

stargazer(m1,m2,m3,m4, type="text",
          dep.var.labels = c("LDP Vote Share"),
          omit.stat = c("f", "ser", "adj.rsq"),
          digits=2,
          covariate.labels = c("Surprise Election", 
                               "Cabinet Faction Ties (%)", "Prime Minister Faction Ties (%)", "Cabinet Faction Ties (%)", "Prime Minister Faction Ties (%)",
                               "Previous LDP Vote Share",  "Previous LDP Seat Share",
                               "Surprise x Cabinet Faction Ties (%)", "Surprise x Prime Minister Faction Ties (%)", "Surprise x Cabinet Faction Ties (%)", "Surprise x Prime Minister Faction Ties (%)"),
          title="Surprise Elections and Intraparty Competition Prior to Reform (1958-1993)")

##
# Table A8: Surprise By-Elections (Open Races)
##

m1 <- felm(ldp_vs ~ surprise + pre_vs | dcode + quarter | 0 | dcode, data=by, subset=open==1)
m2 <- felm(ldp_vs ~ surprise + since_uh + since_local +
             pre_vs + ldp_support_avg_lag1 + pre_seat + gdp_growth_lag1 + inflation_lag1 | dcode + quarter | 0 | dcode, data=by, subset=open==1)
m3 <- felm(opp_recruit ~ surprise + pre_opp_recruit | dcode + quarter | 0 | dcode, data=by, subset=open==1)
m4 <- felm(opp_recruit ~ surprise + since_uh + since_local +
             pre_opp_recruit +  ldp_support_avg_lag1 + pre_seat + gdp_growth_lag1 + inflation_lag1 | dcode + quarter | 0 | dcode, data=by, subset=open==1)
m5 <- felm(opp_hq ~ surprise + pre_opp_hq | dcode + quarter | 0 | dcode, data=by, subset=open==1)
m6 <- felm(opp_hq ~ surprise + since_uh + since_local +
             pre_opp_hq + ldp_support_avg_lag1 + pre_seat + gdp_growth_lag1 + inflation_lag1 | dcode + quarter | 0 | dcode, data=by, subset=open==1)
m7 <- felm(opp_over ~ surprise + pre_opp_over | dcode + quarter | 0 | dcode, data=by, subset=open==1)
m8 <- felm(opp_over ~ surprise + since_uh + since_local +
             pre_opp_over + ldp_support_avg_lag1 + pre_seat + gdp_growth_lag1 + inflation_lag1 | dcode + quarter | 0 | dcode, data=by, subset=open==1)

stargazer(m1,m2,m3,m4,m5,m6,m7,m8, type="text",
          dep.var.labels = c("LDP Vote Share", "Challenger Candidates", "High Quality Challengers", "Coordination Error"),
          omit.stat = c("f", "ser", "adj.rsq"),
          digits=2,
          covariate.labels = c("Surprise By-Election", 
                               "Since Upper House Election", "Since Unified Local Election",
                               "Previous LDP Vote Share", "LDP Support Rating (lagged 1 quarter)", "Previous LDP Seat Share", 
                               "GDP Growth (lagged 1 quarter)", "Inflation (lagged 1 quarter)",
                               "Previous Number of Candidates", "Previous High Quality Candidates", "Previous Coordination Error"),
          title="Surprise By-Elections (Open Races)")

##
# Table A9: Surprise By-Elections (After Electoral Reform)
##

m1 <- felm(ldp_vs ~ surprise + pre_vs | dcode + quarter | 0 | dcode, data=by, subset=year>1994)
m2 <- felm(ldp_vs ~ surprise + since_uh + since_local +
             pre_vs + ldp_support_avg_lag1 + pre_seat + gdp_growth_lag1 + inflation_lag1 | dcode + quarter | 0 | dcode, data=by, subset=year>1994)
m3 <- felm(opp_recruit ~ surprise + pre_opp_recruit | dcode + quarter | 0 | dcode, data=by, subset=year>1994)
m4 <- felm(opp_recruit ~ surprise + since_uh + since_local +
             pre_opp_recruit +  ldp_support_avg_lag1 + pre_seat + gdp_growth_lag1 + inflation_lag1 | dcode + quarter | 0 | dcode, data=by, subset=year>1994)
m5 <- felm(opp_hq ~ surprise + pre_opp_hq | dcode + quarter | 0 | dcode, data=by, subset=year>1994)
m6 <- felm(opp_hq ~ surprise + since_uh + since_local +
             pre_opp_hq + ldp_support_avg_lag1 + pre_seat + gdp_growth_lag1 + inflation_lag1 | dcode + quarter | 0 | dcode, data=by, subset=year>1994)
m7 <- felm(opp_over ~ surprise + pre_opp_over | dcode + quarter | 0 | dcode, data=by, subset=year>1994)
m8 <- felm(opp_over ~ surprise + since_uh + since_local +
             pre_opp_over + ldp_support_avg_lag1 + pre_seat + gdp_growth_lag1 + inflation_lag1 | dcode + quarter | 0 | dcode, data=by, subset=year>1994)

stargazer(m1,m2,m3,m4,m5,m6,m7,m8, type="text",
          dep.var.labels = c("LDP Vote Share", "Challenger Candidates", "High Quality Challengers", "Coordination Error"),
          omit.stat = c("f", "ser", "adj.rsq"),
          digits=2,
          covariate.labels = c("Surprise By-Election", 
                               "Since Upper House Election", "Since Unified Local Election",
                               "Previous LDP Vote Share", "LDP Support Rating (lagged 1 quarter)", "Previous LDP Seat Share", 
                               "GDP Growth (lagged 1 quarter)", "Inflation (lagged 1 quarter)",
                               "Previous Number of Candidates", "Previous High Quality Candidates", "Previous Coordination Error"),
          title="Surprise By-Elections (After Electoral Reform)")

##
# Table A10: Surprise Elections Do Not Undermine LDP Preparedness
##

m1 <- felm(ldp_recruit ~ surprise + pre_ldp_recruit | dcode + quarter | 0 | dcode + year, data=hor) 
m2 <- felm(ldp_recruit ~ surprise + quarter_no + since_uh + since_local +
             pre_ldp_recruit + ldp_support_avg_lag1 + pre_seat + gdp_growth_lag1 + inflation_lag1 | dcode + quarter | 0 | dcode + year, data=hor)
m3 <- felm(ldp_hq ~ surprise + pre_ldp_hq | dcode + quarter | 0 | dcode + year, data=hor)
m4 <- felm(ldp_hq ~ surprise + quarter_no + since_uh + since_local +
             pre_ldp_hq + ldp_support_avg_lag1 + pre_seat + gdp_growth_lag1 + inflation_lag1 | dcode + quarter | 0 | dcode + year, data=hor)
m5 <- felm(ldp_over ~ surprise + pre_ldp_over | dcode + quarter | 0 | dcode + year, data=hor, subset=hor$year < 1994)
m6 <- felm(ldp_over ~ surprise + quarter_no + since_uh + since_local +
             pre_ldp_over + ldp_support_avg_lag1 + pre_seat + gdp_growth_lag1 + inflation_lag1 | dcode + quarter | 0 | dcode + year, data=hor, subset=hor$year < 1994)
m7 <- felm(ldp_exp_pc ~ surprise + pre_ldp_exp_pc | dcode + quarter | 0 | dcode + year, data=hor)
m8 <- felm(ldp_exp_pc ~ surprise + quarter_no + since_uh + since_local +
             pre_ldp_exp_pc + ldp_support_avg_lag1 + pre_seat + gdp_growth_lag1 + inflation_lag1 | dcode + quarter | 0 | dcode + year, data=hor) 

stargazer(m1,m2,m3,m4,m5,m6,m7,m8, type="text",
          dep.var.labels = c("Recruited Candidates", "High Quality Recruits", "Coordination Error", "Campaign Spending"),
          omit.stat = c("f", "ser", "adj.rsq"),
          digits=2,
          covariate.labels = c("Surprise Election", 
                               "Linear Time Trend", "Quarters Since Upper House Election", "Quarters Since Unified Local Election",
                               "Previous Number of Candidates", "LDP Support Rating (lagged 1 quarter)", "Previous LDP Seat Seat Share",
                               "GDP Growth (lagged 1 quarter)", "Inflation (lagged 1 quarter)",
                               "Previous High Quality Candidates", "Previous Coordination Error", "Previous Campaign Spending"),
          title="Surprise Elections Do Not Undermine LDP Preparedness")

##
# Table A11: Surprise By-Elections Benefit the LDP
##

m1 <- felm(ldp_vs ~ surprise + pre_vs | dcode + quarter | 0 | dcode + year, data=by)
m2 <- felm(ldp_vs ~ surprise + since_uh + since_local +
             pre_vs + ldp_support_avg_lag1 + pre_seat + gdp_growth_lag1 + inflation_lag1 | dcode + quarter | 0 | dcode + year, data=by)
m3 <- felm(ldp_recruit ~ surprise + pre_ldp_recruit | dcode + quarter | 0 | dcode + year, data=by)
m4 <- felm(ldp_recruit ~ surprise + since_uh + since_local +
             pre_ldp_recruit + ldp_support_avg_lag1 + pre_seat + gdp_growth_lag1 + inflation_lag1 | dcode + quarter | 0 | dcode + year, data=by) 
m5 <- felm(ldp_hq ~ surprise + pre_ldp_hq | dcode + quarter | 0 | dcode + year, data=by)
m6 <- felm(ldp_hq ~ surprise + since_uh + since_local +
             pre_ldp_hq + ldp_support_avg_lag1 + pre_seat + gdp_growth_lag1 + inflation_lag1 | dcode + quarter | 0 | dcode + year, data=by) 
m7 <- felm(ldp_over ~ surprise | dcode + quarter | 0 | dcode + year, data=by, subset=year < 1994)
m8 <- felm(ldp_over ~ surprise + since_uh + since_local +
            ldp_support_avg_lag1 + pre_seat + gdp_growth_lag1 + inflation_lag1 | dcode + quarter | 0 | dcode + year, data=by, subset=year<1994) 

stargazer(m1,m2,m3,m4,m5,m6,m7,m8, type="text",
          dep.var.labels = c("LDP Vote Share", "Recruited Candidates", "High Quality Recruits", "Coordination Error"),
          omit.stat = c("f", "ser", "adj.rsq"),
          digits=2,
          covariate.labels = c("Surprise By-Election", 
                               "Since Upper House Election", "Since Unified Local Election",
                               "Previous LDP Vote Share", "LDP Support Rating (lagged 1 quarter)", "Previous LDP Seat Share", 
                               "GDP Growth (lagged 1 quarter)", "Inflation (lagged 1 quarter)",
                               "Previous Number of Candidates", "Previous High Quality Candidates"),
          title="Surprise By-Elections Benefit the LDP")




